package dao

import (
	"gorm.io/gorm"
	"xlq-demo/app/model"
	"xlq-demo/library/db"
)

type DeviceBindHistoryDao struct{}

func NewDeviceBindHistoryDao() *DeviceBindHistoryDao {
	return new(DeviceBindHistoryDao)
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) Create(deviceBindHistory *model.DeviceBindHistory) error {
	return db.GetMaster().Create(deviceBindHistory).Error
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) Delete(id interface{}) error {
	return db.GetMaster().Where("id=?", id).Delete(new(model.DeviceBindHistory)).Error
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) Update(id string, dataMap map[string]interface{}) error {
	return db.GetMaster().Model(new(model.DeviceBindHistory)).Where("id=?", id).Updates(dataMap).Error
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) Find(id interface{}) (*model.DeviceBindHistory, error) {
	deviceBindHistory := new(model.DeviceBindHistory)
	err := db.GetSlave().Where("id=?", id).Take(deviceBindHistory).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return deviceBindHistory, err
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) Get(ids []interface{}) ([]model.DeviceBindHistory, error) {
	var deviceBindHistorys []model.DeviceBindHistory
	err := db.GetSlave().Find(&deviceBindHistorys, ids).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return deviceBindHistorys, err
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) All() ([]model.DeviceBindHistory, error) {
	var deviceBindHistorys []model.DeviceBindHistory
	err := db.GetSlave().Find(&deviceBindHistorys).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return deviceBindHistorys, err
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) FindByWhere(wheres []string, params []interface{}, orders string) (*model.DeviceBindHistory, error) {
	whereLen := len(wheres)
	deviceBindHistory := new(model.DeviceBindHistory)
	db := db.GetSlave()
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Take(&deviceBindHistory).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return deviceBindHistory, err
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) GetByWhere(wheres []string, params []interface{}, orders string) ([]model.DeviceBindHistory, error) {
	whereLen := len(wheres)
	var deviceBindHistorys []model.DeviceBindHistory
	db := db.GetSlave()
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Find(&deviceBindHistorys).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return deviceBindHistorys, err
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) CountByWhere(wheres []string, params []interface{}) (int64, error) {
	whereLen := len(wheres)
	deviceBindHistory := new(model.DeviceBindHistory)
	db := db.GetSlave().Model(deviceBindHistory)
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	var total int64
	err := db.Count(&total).Error
	return total, err
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) ListByWhere(wheres []string, params []interface{}, orders string, offset, limit int) ([]model.DeviceBindHistory, error) {
	var deviceBindHistorys []model.DeviceBindHistory
	db := db.GetSlave()
	for i := 0; i < len(wheres); i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Offset(offset).Limit(limit).Find(&deviceBindHistorys).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return deviceBindHistorys, err
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) Raw(sql string, params []interface{}, out interface{}) error {
	err := db.GetSlave().Raw(sql, params...).Scan(out).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return err
}

func (deviceBindHistoryDao *DeviceBindHistoryDao) Exec(sql string, params []interface{}) error {
	return db.GetSlave().Exec(sql, params).Error
}
